function delay3

% solve: r=-exp(lam*r) where r=A+iB
clf

global lam

N=10000;
lam=exp(-1);

AA(1)=-exp(1);
BB(1)=0;

ep=0.001;
lam=lam+ep;
B=sqrt(2)*exp(1.5)*ep^0.5;
A=AA(1)+5*exp(2)*ep/3;
for i=1:N
    if AA(i)>0
        B=solve(@f,0.5*B,B);
    else
        B=solve(@f,0.0001,pi-0.001);
    end
    AA(i+1)=-B*cos(lam*B)/sin(lam*B);
    BB(i+1)=B;
    % if lam>2
    %     break
    % end
    %lam=lam+0.001;
    if AA(i+1)>0 && BB(i+1)<0.3
        lam=lam+0.026;
    else
        lam=lam+0.001;
    end
end
lam

figure(1)
% get(gcf)
set(gcf,'Position', [25 1115 658 230])
clf
plot(AA,BB,'b','LineWidth',2)
hold on
plot([-2*exp(1) -0.01],[0 0],'b','LineWidth',2)
box on
grid on
plot(AA,-BB,'b','LineWidth',2)
plot([-exp(1) 0.5],[0 0],'k')
plot([0 0],[-2 2],'k')
axis([-5 0.41 -2 2])

set(groot,'defaultAxesTickLabelInterpreter','latex');
set(gca,'XTick',[-exp(1) 0])
set(gca,'XTickLabel',{'$-e^1$';'0'})

set(gca,'YTick',[-2 -1 0 1 2])
%set(gca,'YTickLabel',{'$\pi/2$';'$-1$';'0';'$1$';'$\pi/2$'})

arrowhead([-1 -1.1],[0 0],'b',[0.8 1.5],2);
arrowhead([-4.1 -4],[0 0],'b',[0.8 1.5],2);
ii=200;
arrowhead([AA(ii) AA(ii+1)],[BB(ii) BB(ii+1)],'b',[0.8 1.5],2);
arrowhead([AA(ii) AA(ii+1)],[-BB(ii) -BB(ii+1)],'b',[0.8 1.5],2);

xlabel('A')
ylabel('B')
set(gca,'FontSize',18,'FontWeight','bold')

%exportgraphics(gca,'/Users/mark/Desktop/delay.pdf')


% figure(2)
%
% N=100;
% lam=-3*pi/2;
%
% AA(1)=0;
% BB(1)=1;
%
% ep=0.001;
% lam=lam+ep;
% B=1;
% A=0;
% for i=1:N
%     B=solve(@f,0.0001,pi-0.001);
%     AA(i+1)=-B*cos(lam*B)/sin(lam*B);
%     BB(i+1)=B;
%     if lam>2
%         break
%     end
%     lam=lam+0.0001;
%     % if B<1
%     %     lam=lam+0.001;
%     % else
%     %     lam=lam+0.01;
%     % end
% end
% lam
%
% clf
% plot(AA,BB,'b','LineWidth',2)
% hold on
% plot([-2*exp(1) -0.01],[0 0],'b','LineWidth',2)
% box on
% grid on
% plot(AA,-BB,'b','LineWidth',2)
% set(gca,'FontSize',18,'FontWeight','bold')

function z=f(b)
global lam
A=-b*cos(lam*b)/sin(lam*b);
z=b-exp(-lam*A)*sin(lam*b);

